# coding=utf-8
__author__ = 'st316'
'''
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
(you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
==================================
另一例子：
A           G
B       F   H
C   E       I
D           J
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
注意：nrows==1时候需要特殊处理
'''


class Solution:
    # @return a string
    def convert(self, s, nRows):
        if nRows == 1:
            return s
        rows = ['' for x in xrange(nRows)]
        i = 0
        flag = True  # i+=/i--
        for c in s:
            rows[i] += c
            i += 1 if flag else -1
            if i == nRows:
                i -= 2
                flag = False
            if i == -1:
                i += 2
                flag = True
        news = ''
        for row in rows:
            news += row
        return news


if __name__ == '__main__':
    s = Solution()
    print s.convert("AB", 1)